import math  
from loguru import logger
from app.core.conf import ST


class ClassifyState:

    __slots__ = ('ocr_raw_list','ocr_filter_list','state','state_text','state_color','classify_flag','img_draw')

    def __init__(self,ocr_raw_list) -> None:
        self.ocr_raw_list = ocr_raw_list # 可能是温度或者湿度的字符串数组
        self.ocr_filter_list = []
        self.state=None #过滤之后得到的ocr state
        self.state_text = '' #状态文字
        self.state_color= '' #状态颜色
        self.classify_flag = False # 识别的信息是否分类号的标志位,默认为False 
        self.img_draw=None


    def classify(self):
        self.ocr_filter_list = [s for s in self.ocr_raw_list if  ClassifyState.state_filter_save(s.get('t')) ]
        length = len(self.ocr_filter_list)
        if length==1:
            logger.debug('检测到蓝色圆盘状态')
            self.state = self.ocr_filter_list[0]
            self.set_state_text()
            self.set_state_color()
            self.classify_flag = True 
        elif length==2:
            res = any([True for item in self.ocr_filter_list if '维护' in item['t'] or '调试' in item['t']])  
            if res:
                logger.debug('检测到蓝色圆盘状态')
                self.state = self.ocr_filter_list[0]
                self.set_state_text()
                self.set_state_color()
                self.classify_flag = True 
            else:
                logger.debug('没有检测到蓝色圆盘状态')
                self.classify_flag = False 
        else:
            logger.debug('没有检测到蓝色圆盘状态')
            self.classify_flag = False 



    def is_run_state(self):
        if self.state_text == '运行':
            return True
        else:
            return False


    def set_state_text(self):
        self.state_text = self.state.get('t')

    def set_state_color(self):
        self.state_color = ST.state_color.get(self.state_text)

    @staticmethod
    def state_filter_save(content):
        drop_flag_list = [ keyword in content for keyword in ST.state_filter_save]
        if any(drop_flag_list): # 至少有一个为真,就返回真值
            return content
        return None
